home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / misc / wt004wc / wt.c < prev   
Encoding:
C/C++ Source or Header  |  1994-05-27  |  5.0 KB  |  182 lines

  1. /*
  2. **  wt -- a 3d game engine
  3. **
  4. **  Copyright (C) 1994 by Chris Laurel
  5. **  email:  claurel@mr.net
  6. **  snail mail:  Chris Laurel, 5700 W Lake St #208,  St. Louis Park, MN  55416
  7. **
  8. **  This program is free software; you can redistribute it and/or modify
  9. **  it under the terms of the GNU General Public License as published by
  10. **  the Free Software Foundation; either version 2 of the License, or
  11. **  (at your option) any later version.
  12. **
  13. **  This program is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. **  GNU General Public License for more details.
  17. **
  18. **  You should have received a copy of the GNU General Public License
  19. **  along with this program; if not, write to the Free Software
  20. **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. /* Frames per second counter added for MS-DOS version. All code specific
  24.  * this counter is inside #ifdef MSDOS statements.
  25.  * Petteri Kangaslampi <pekanga@freeport.uwasa.fi>
  26. */
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include "wt.h"
  32. #include "error.h"
  33. #include "fixed.h"
  34. #include "view.h"
  35. #include "texture.h"
  36. #include "table.h"
  37. #include "world.h"
  38. #include "worldfile.h"
  39. #include "framebuf.h"
  40. #include "render.h"
  41. #include "graphics.h"
  42. #include "input.h"
  43.  
  44. View *view;
  45.  
  46. #ifdef MSDOS
  47. #include <time.h>
  48. #endif
  49.  
  50.  
  51. int main(int argc, char *argv[])
  52. {
  53.      World *w;
  54.      FILE *fp;
  55.      Boolean quit = False;
  56.      Intent *intent;
  57.      fixed v = FIXED_ZERO;
  58.      double vx = 0.0, vy = 0.0, va = 0.0;
  59.      #ifdef MSDOS
  60.      long       frames;
  61.      time_t     starttime, endtime;
  62.      #endif
  63.  
  64.  
  65.      if (argc != 2) {
  66.       fprintf(stderr, "Usage:  wt <world file>\n");
  67.       exit(EXIT_FAILURE);
  68.      }
  69.  
  70.      if ((fp = fopen(argv[1], "r")) == NULL) {
  71.       perror(argv[1]);
  72.       exit(EXIT_FAILURE);
  73.      }
  74.      w = read_world_file(fp);
  75.      fclose(fp);
  76.  
  77.      init_graphics();
  78.      init_renderer(SCREEN_WIDTH, SCREEN_HEIGHT);
  79.      init_input_devices();
  80.  
  81.      /* setup view */
  82.      view = new_view(fixdiv(FIXED_2PI, INT_TO_FIXED(4)));
  83.  
  84.      view->x = FIXED_ZERO;
  85.      view->y = FIXED_ZERO;
  86.      view->height = FIXED_ONE;
  87.      view->angle = FIXED_ZERO;
  88.  
  89.      #ifdef MSDOS
  90.      starttime = time(NULL);
  91.      frames = 0;
  92.      #endif
  93.  
  94.      while (!quit) {
  95.       double sin_facing, cos_facing;
  96.  
  97.       render(w, view);
  98.       #ifdef MSDOS
  99.       frames++;
  100.       #endif
  101.       intent = read_input_devices();
  102.  
  103.       /* This block code is a hack to do acceleration and deceleration. */
  104.       if (fabs(vx) > fabs(intent->force_x)) {
  105.            if (vx < 0.0)
  106.             vx = MIN(vx + 0.1, intent->force_x);
  107.            else
  108.             vx = MAX(vx - 0.1, intent->force_x);
  109.       } else if (fabs(vx) < fabs(intent->force_x)) {
  110.            vx += intent->force_x / 5.0;
  111.            if (fabs(vx) > fabs(intent->force_x))
  112.             vx = intent->force_x;
  113.       }
  114.       if (fabs(vy) > fabs(intent->force_y)) {
  115.            if (vy < 0.0)
  116.             vy = MIN(vy + 0.1, intent->force_y);
  117.            else
  118.             vy = MAX(vy - 0.1, intent->force_y);
  119.       } else if (fabs(vy) < fabs(intent->force_y)) {
  120.            vy += intent->force_y / 5.0;
  121.            if (fabs(vy) > fabs(intent->force_y))
  122.             vy = intent->force_y;
  123.       }
  124.       if (fabs(vy) > fabs(intent->force_y)) {
  125.            if (vy < 0.0)
  126.             vy = MIN(vy + 0.1, intent->force_y);
  127.            else
  128.             vy = MAX(vy - 0.1, intent->force_y);
  129.       } else if (fabs(vy) < fabs(intent->force_y)) {
  130.            vy += intent->force_y / 5.0;
  131.            if (fabs(vy) > fabs(intent->force_y))
  132.             vy = intent->force_y;
  133.       }
  134.       /* Angular deceleration here is weird and unrealistic, but it feels
  135.       **   right to me.
  136.       */
  137.       if (fabs(va) > fabs(intent->force_rotate))
  138.            va *= 0.6;
  139.       else if (fabs(va) < fabs(intent->force_rotate)) {
  140.            va += intent->force_rotate / 8.0;
  141.            if (fabs(va) > fabs(intent->force_rotate))
  142.             va = intent->force_rotate;
  143.       }
  144.       view->angle += FLOAT_TO_FIXED(0.3 * va);
  145.       sin_facing = sin(FIXED_TO_FLOAT(view->angle));
  146.       cos_facing = cos(FIXED_TO_FLOAT(view->angle));
  147.  
  148.       view->x += FLOAT_TO_FIXED(0.8 * vx * cos_facing);
  149.       view->y += FLOAT_TO_FIXED(0.8 * vx * sin_facing);
  150.       view->x += FLOAT_TO_FIXED(0.8 * vy * -sin_facing);
  151.       view->y += FLOAT_TO_FIXED(0.8 * vy * cos_facing);
  152.       if (view->height > FIXED_ONE)
  153.            v -= FIXED_ONE / 16;
  154.       view->height += v;
  155.       if (view->height < FIXED_ONE) {
  156.            v = FIXED_ZERO;
  157.            view->height = FIXED_ONE;
  158.       }
  159.       while (intent->n_special--) {
  160.            if (intent->special[intent->n_special] == INTENT_END_GAME)
  161.             quit = True;
  162.            else
  163.             v = FIXED_ONE / 2;
  164.       }
  165.      }
  166.  
  167.      #ifdef MSDOS
  168.      endtime = time(NULL);
  169.      #endif
  170.  
  171.      end_input_devices();
  172.      end_graphics();
  173.  
  174.      #ifdef MSDOS
  175.      printf("%li frames in %lu seconds - %.2f frames per second",
  176.         (long) frames, (long) endtime - starttime,
  177.         (float) frames / (float) (endtime-starttime));
  178.      #endif
  179.  
  180.      return EXIT_SUCCESS;
  181. }
  182.